<?php

namespace zhtfrontend\models\datas;

use common\components\extenders\MenuManage;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%tutor}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $picture
 * @property string $appellation
 * @property string $description
 * @property integer $create_at
 */
class Tutor extends \zhtfrontend\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%tutor}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['description'], 'string'],
            [['created_at','status'], 'integer'],
            [['name'], 'string', 'max' => 255],
            [['picture'], 'string', 'max' => 200],
            [['appellation'], 'string', 'max' => 50],
            [['name', 'appellation', 'description','status'], 'required']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'name' => Yii::t('zhtfrontend', '导师名称'),
            'picture' => Yii::t('zhtfrontend', '照片'),
            'appellation' => Yii::t('zhtfrontend', '称谓'),
            'description' => Yii::t('zhtfrontend', '简介'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'status' => Yii::t('zhtfrontend', '状态'),
        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['picture'] = function () {
            if($this->picture == null){
                return '<img src="\images\images\img_default_face.png" style="width:50px;height:50px;">';
            } else {
                return '<img src="'.$this->picture.'" style="width:50px;height:50px;">';
            }
        };
        $fields['status'] = function (){
            switch ($this->status){
                case 0:
                    return Yii::t('zhtfrontend', '禁用');
                default:
                    return Yii::t('zhtfrontend','启用');
            }
        };
        return $fields;
    }

    public function extraFields(){

        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    [
                      'delete' => function ($data) {
                         $goods = Goods::find()->where(['tutor_id'=>$this->id])->one();
                        if ($goods != null) {
                            return '<button class="yx-btn-disabled" type="button">删除</button>';
                        } else {
                            return '';
                        }
                        },
                      'update'=> function(){
                            return '';
                        },

                    ]
                )->create();
            },
        ];
    }
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }
}
